<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/


/** @see Zead_Player_Action **/
require_once 'Zead/Player/Action.php';

/**
 * This class implements the use of the print version for one or more contents 
 * recorded in manifest, according to choiced option.
 *
 * 
 * @category   Zead
 * @package    Zead_Player
 * @subpackage Zead_Player_Action
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License 
 */
class Zead_Player_Action_Print extends Zead_Player_Action 
{
	/** @var Zead_Html **/
	protected $html = null;
	
	/** @var string **/
	protected $htmlContent = null;
	
	/** @var array **/
	protected $toc  = array();
	
	/**
	 * Dispatching the action
	 *
	 */
	public function dispatch() 
	{		
		if ($this->getPlayer()->getParam('useCache') == true && $this->getRequest()->getParam('nocache', 0) == 0) {
			$this->cacheRender();
		} else {
			$this->htmlRender();
		}
		$this->getResponse()->setBody($this->htmlContent);		
	}
	
	/**
	 * Render the print version from a rendered cached version
	 * @return void
	 */
	protected function cacheRender()
	{
		$cacheId = md5($this->getPlayer()->credential['baseUri']);
		// Opções do cache
		$frontendOptions = array(
			'lifetime' => 120, // Renovando o cache de 2 em 2 minutos 
			'automatic_serialization' => false
		);
		$backendOptions = array(
			'cache_dir' => $this->getPlayer()->getParam('cacheDir')
		);
		$cache = Zend_Cache::factory('Output', 'File', $frontendOptions, $backendOptions);
			
		// Gerando a saída do cache, se não  houver nada gravado.
		if (!($this->htmlContent = $cache->load($cacheId))) {
			$this->htmlRender();
			$cache->save($this->htmlContent, $cacheId);				
		} 
	}
	
	/**
	 * Render the html for print
	 * @return void
	 */
	protected function htmlRender()
	{
		// Process all contents
		$this->_processContentStaticFiles($this->manifest->getObject()->getContentsList(), &$this->toc);	
		
		// Process toc
/*		if (count($this->toc)) {
			$this->html = $this->_processToc($this->toc) . $this->html;
		}*/
		
		// Parse global HTML
		$this->html = new Zead_Html();
		$this->html->loadHTML($this->htmlContent);
		$this->html->headTitle($this->manifest->getTitle());
		$this->html->formatTidy = true;	
		$this->htmlContent = $this->html->saveHTML();	
	}
	
	protected function _processContentStaticFiles($contentsList, $toc = array(), $indexLevel = 1)
	{
		foreach ($contentsList as $content) {
			$filesRange = Zead_Manifest_Util::getContentStaticFiles($content, $this->getPlayer()->credential['basePath']);
			$pagesRange = Zead_Manifest_Util::getContentHttpPages($content, $this->getPlayer()->credential['baseUri']);
			$targetId = 'toc'. uniqid();
			//$this->html .= '<h'. $indexLevel . ' id="'. $targetId . '">'. $content->getTitle() . '</h'. $indexLevel . '>';							
			
			$pageNumber = min($filesRange, $pagesRange);
			// Process filesRange
			foreach ($filesRange as $key => $file) {
				$page = $pagesRange->getPage($key + 1);
				if (file_exists($file->url)) {
					$html = new Zead_Html();
					$html->load($file->url);
					$html->linkPath(dirname($page->url));
					$html->formatTidy = true;
					$this->htmlContent .= $html->saveHTML(true);
					$this->htmlContent .= '<div style="height:350px">.</div>';
				}
			}
			// Register an entry in toc
			$tocEntry = new stdClass();
			$tocEntry->title = $content->getTitle();
			$tocEntry->target = $targetId;
			$tocEntry->toc = array();
			$toc[] = $tocEntry;
			
			// Process contents list for this content
			$this->_processContentStaticFiles($content->getContentsList(), &$tocEntry->toc, $indexLevel + 1);
		}
	}
	
	protected function _processToc($toc) 
	{
		$code = '<ul class="toc">';
		
		foreach ($toc as $index) {
			$code .= '<li><a href="#'. $index->target . '">'. $index->title . '</li>';
			$code .= $this->_processToc($index->toc);
		}
		$code .= '</ul>';
		return $code;
	}	
}
